// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.googlecodesearch;

import com.google.inject.Inject;
import com.google.jessewilson.codesearch.api.Query;
import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParameters;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParametersEditor;
import com.google.jessewilson.codesearch.ide.QueryHistory;
import com.google.jessewilson.codesearch.ui.HistoryField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;

import ca.odell.glazedlists.FunctionList;

/**
 * @author jessewilson
 */
class GcsSearchProviderParametersEditor
    implements SearchProviderParametersEditor {

  private final HistoryField<Query> fileTypes;
  private final HistoryField<Query> packages;
  private final JCheckBox caseSensitive;

  private final JPanel panel;

  @Inject
  public GcsSearchProviderParametersEditor(QueryHistory queryHistory) {
    fileTypes = new HistoryField<Query>(
        new FunctionList.Function<Query,String>() {
          public String evaluate(Query query) {
            GcsSearchProviderParameters queryExtra = (GcsSearchProviderParameters) query.getSearchProviderParameters();
            return queryExtra != null ? queryExtra.getFileTypes() : null;
          }
        });
    packages = new HistoryField<Query>(
        new FunctionList.Function<Query,String>() {
          public String evaluate(Query query) {
            GcsSearchProviderParameters queryExtra = (GcsSearchProviderParameters) query.getSearchProviderParameters();
            return queryExtra != null ? queryExtra.getPackages() : null;
          }
        });
    caseSensitive = new JCheckBox("Case Sensitive");

    // tmao(2007-09-11): Commenting this out pending discussion on how we're
    // going to handle externalizing state.

//    fileTypes.setHistory(queryHistory.getSnapshot());
//    packages.setHistory(queryHistory.getSnapshot());

    FormLayout formLayout = new FormLayout(
        "right:min, 3px, left:pref:grow",
        "min, 3px, min, 3px, min");
    panel = new JPanel(formLayout);

    CellConstraints cc = new CellConstraints();
    panel.add(new JLabel("File Types:"), cc.xy(1, 1));
    panel.add(fileTypes.getComponent(), cc.xy(3, 1, "fill, fill"));
    panel.add(new JLabel("Packages:"), cc.xy(1, 3));
    panel.add(packages.getComponent(), cc.xy(3, 3, "fill, fill"));
    panel.add(caseSensitive, cc.xy(3, 5, "fill, fill"));
  }


  public void setAction(Action action) {
    fileTypes.setAction(action);
    packages.setAction(action);
  }

  public JPanel getComponent() {
    return panel;
  }

  public void setValue(SearchProviderParameters value) {
    assert(value != null);

    GcsSearchProviderParameters gcsQueryExtra =
        (GcsSearchProviderParameters) value;

    this.fileTypes.setValue(gcsQueryExtra.getFileTypes());
    this.packages.setValue(gcsQueryExtra.getPackages());
    this.caseSensitive.setSelected(gcsQueryExtra.isCaseSensitive());
  }

  public SearchProviderParameters getValue() {
    return new GcsSearchProviderParameters(
        fileTypes.getValue(),
        packages.getValue(),
        caseSensitive.isSelected(),
        true);
  }


  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() { public void run() {
          JPanel panel = new GcsSearchProviderParametersEditor(null).panel;
          panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

          JFrame frame = new JFrame();
          frame.getContentPane().add(panel);
          frame.setSize(400, 200);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }});
  }


  public SearchProvider getSearchProvider() {
    return new GcsSearchProvider();
  }
}
